$(document).ready(function(){

    function Slider(container,nav){
        this.container      = container;
        this.nav            = nav.show(); //show navigation if hidden
        this.imgs           = this.container.find('img');
        this.imgWidth       = this.imgs[0].width; //570
        this.imgLen         = this.imgs.length; //5
        this.current        = 0;

        this.events.click.call(this);
    }

    Slider.prototype.transition = function ( coords ){
        this.container.animate({
            'margin-left' : coords || -(this.current * this.imgWidth)
        });
    };

    Slider.prototype.setCurrent = function( dir ){
        var pos = this.current;
//        ( dir === 'next') ? this.current++ : this.current--;the same as next line
        pos += (~~( dir === 'next') || -1); // ~~ converts true,false into 1,0

        this.current = (pos < 0) ? this.imgLen - 1 : pos % this.imgLen;
    };

    Slider.prototype.events = {
        click : function(){
            var self = this;

            self.nav.find('button').on('click',function(){
                var current = self.setCurrent($(this).data('dir'));
                self.transition();
            });
        }
    };

    new Slider($('.slider ul'),$('.slider_nav'));


});
